/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.Arkham;

import java.io.*;
import java.util.ArrayList;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

public class GameFile{
	public static Party party = new Party();
	public static final int numStoryFlags = 10;
	public static boolean[] storyFlag = new boolean[numStoryFlags];
	static File file = new File(Environment.getExternalStorageDirectory(),"gameSave.txt");


	/* STORY COMPLETION FLAGS
	 *   0 - Introduction
	 *   1 - 1st fight with Matt
	 */


	public void saveGameToFile() throws IOException
	{

		////FOR TESTING
		//		GameFile.party.characters[1].newGame();
		//		GameFile.party.characters[1].changeName("Phil the Pesko");
		//		GameFile.party.characters[1].changeWorstFear("Tweek with hair");
		////

		BufferedWriter br = new BufferedWriter(new FileWriter(file));						//Set up input
		int i=0;																			//Index of character array
		while (i<party.characters.length && !party.characters[i].getName().equals(""))		//Loop until the array length or end of characters
		{
			br.write("*charS*\n");
			br.write(party.characters[i].getName()			+"\n");							//Character's name
			br.write(party.characters[i].getWorstFear()		+"\n");							//Character's fear 
			br.write(party.characters[i].getLevel()			+"\n");							//Character's level
			br.write(party.characters[i].getExperience()	+"\n");							//Character's level

			int[] stats = party.characters[i].getBaseStats();								//Initializes stats as the array of base stats per character
			for (int j=0 ; j<Constants.TOTAL_STATS ; j++)									//Loops through all stats
			{
				br.write(stats[j]							+"\n");							//Character's stats
			}
			i++;																			//Increments index of character array
		}
		//br.write("*charE*\n");															//Prints end of character info
		br.write(party.inventory.getMoney()					+"\n");							//Prints party's total money

		int k=0;																			//Index of inventory array
		while (k<party.inventory.size())													//Loop until array length
		{
			br.write(party.inventory.get(k).getName()		+"\n");							//Print the item name

			if (party.inventory.isEquipped(k))												//If its equipped...
			{
				br.write("*equipT*\n");
				br.write(party.inventory.get(k).getEquippedBy()+"\n");						//Print who has item equipped
				br.write(party.inventory.get(k).getEquippedAt()+"\n");						//Print where the item is equipped on character
			}
			k++;																			//Increments index of inventory array
			br.write("*equipE*\n");
		}
		br.write("*equipS*\n");
		int l=0;																			//Index of storyFlags
		while (l<numStoryFlags)																//Loop "numStoryFlags" times
		{
			br.write(Boolean.toString(storyFlag[l])+"\n");									//Print the story flag value (true/false)
			l++;																			//Increment index of storyFlag array
		}

		main.savedGame = true;

		br.flush();																			//Finish input capturing
		br.close();																			//Close writing
		getGameFile();
	}


	public static void getGameFile() throws IOException
	{
		BufferedReader reader = new BufferedReader(new FileReader(file));
		Stats baseStats = new Stats();
		String temp;
		if (file.exists())
		{
			temp = reader.readLine();

			//		while(temp!=null)
			//		{
			//			Log.i("SaveFile",temp);
			//			temp=reader.readLine();
			//		}

			if (temp != null)
			{
				int i=0;
				do
				{
					party.NewGame();
					party.characters[i].changeName(reader.readLine());
					party.characters[i].changeWorstFear(reader.readLine());
					party.characters[i].setLevel(Integer.parseInt(reader.readLine()));
					party.characters[i].setEXP(Integer.parseInt(reader.readLine()));
					baseStats.setStrength(Integer.parseInt(reader.readLine()));
					baseStats.setAgility(Integer.parseInt(reader.readLine()));
					baseStats.setVitality(Integer.parseInt(reader.readLine()));
					baseStats.setMagic(Integer.parseInt(reader.readLine()));
					baseStats.setAttackPower(Integer.parseInt(reader.readLine()));
					baseStats.setHitPercentage(Integer.parseInt(reader.readLine()));
					baseStats.setDefense(Integer.parseInt(reader.readLine()));
					baseStats.setEvade(Integer.parseInt(reader.readLine()));
					baseStats.setMagicDefense(Integer.parseInt(reader.readLine()));
					baseStats.setMagicEvade(Integer.parseInt(reader.readLine()));
					baseStats.setMaxHealth(Integer.parseInt(reader.readLine()));
					baseStats.setCurrentHealth(Integer.parseInt(reader.readLine()));
					baseStats.setMaxMagic(Integer.parseInt(reader.readLine()));
					baseStats.setCurrentMagic(Integer.parseInt(reader.readLine()));	
					temp=reader.readLine();
					i++;
				} while(temp.equals("*charS*"));

				party.inventory.setMoney(Integer.parseInt(temp));

				int j=0;
				String by="";
				int at=0;
				do
				{
					temp=reader.readLine();
					if (!temp.equals("*equipS*"))
					{
						if (!temp.equals("*equipE*"))
						{
							getItem(temp);
							temp=reader.readLine();
							if(temp.equals("*equipT*"))
							{
								by=reader.readLine();
								at=Integer.parseInt(reader.readLine());
								party.inventory.equip(j, by, at);
							}
							temp=reader.readLine();
						}
					}
				} while(temp.equals("*equipE*"));

				int m=0;
				boolean temp2=false;
				String temp1="";
				while(m<numStoryFlags) 
				{
					temp1=reader.readLine();
					Log.i("SaveFile",temp1);
					temp2=Boolean.parseBoolean(temp1);
					GameFile.storyFlag[m]=temp2;
					m++;
				}
			}
			reader.close();
		}
	}

	public static void getItem(String itemName)
	{
		//Weapons
		if 		(itemName.equals("Dull Pencil"))
		{party.inventory.gainItem(Armory.DullPencil());}
		else if (itemName.equals("Pencil"))
		{party.inventory.gainItem(Armory.Pencil());}
		else if (itemName.equals("MechanicalPencil"))
		{party.inventory.gainItem(Armory.MechanicalPencil());}
		else if (itemName.equals("Pen"))
		{party.inventory.gainItem(Armory.Pen());}
		else if (itemName.equals("Calculator"))
		{party.inventory.gainItem(Armory.Calculator());}
		else if (itemName.equals("ScientificCalculator"))
		{party.inventory.gainItem(Armory.ScientificCalculator());}
		else if (itemName.equals("GraphingCalculator"))
		{party.inventory.gainItem(Armory.GraphingCalculator());}
		//Shields
		else if (itemName.equals("UsedEraser"))
		{party.inventory.gainItem(Armory.UsedEraser());}
		else if (itemName.equals("Eraser"))
		{party.inventory.gainItem(Armory.Eraser());}
		else if (itemName.equals("MechanicalEraser"))
		{party.inventory.gainItem(Armory.MechanicalEraser());}
		//Headgear
		else if (itemName.equals("DunceCap"))
		{party.inventory.gainItem(Armory.DunceCap());}
		else if (itemName.equals("BallCap"))
		{party.inventory.gainItem(Armory.BallCap());}
		else if (itemName.equals("Beanie"))
		{party.inventory.gainItem(Armory.Beanie());}
		//Armor
		else if (itemName.equals("StreetClothes"))
		{party.inventory.gainItem(Armory.StreetCloths());}
		else if (itemName.equals("SchoolUniform"))
		{party.inventory.gainItem(Armory.SchoolUniform());}
		else if (itemName.equals("DressSchoolUniform"))
		{party.inventory.gainItem(Armory.DressSchoolUniform());}
		//Feet
		else if (itemName.equals("Sneakers"))
		{party.inventory.gainItem(Armory.Sneakers());}
		else if (itemName.equals("UniformShoes"))
		{party.inventory.gainItem(Armory.UniformShoes());}
		//Accessories
		else if (itemName.equals("ClassRing"))
		{party.inventory.gainItem(Armory.ClassRing());}
		//usable
		else if (itemName.equals("ExtraCreditHomework"))
		{party.inventory.gainItem(Armory.ExtraCreditHomework());}

	}
}